"""
轮播图序列化器 - 门户网站
"""

from rest_framework import serializers
from pecco_site.models import CarouselItem


class CarouselSerializer(serializers.Serializer):
    """轮播图序列化器"""
    id = serializers.IntegerField()
    image = serializers.SerializerMethodField()
    title = serializers.CharField()
    subtitle = serializers.CharField()
    cta_text = serializers.CharField()
    link = serializers.CharField()
    
    def get_image(self, obj):
        """获取轮播图图片"""
        if hasattr(obj, 'image_url'):
            return obj.image_url
        if hasattr(obj, 'image') and obj.image:
            request = self.context.get('request')
            if request:
                return request.build_absolute_uri(obj.image.url)
            return obj.image.url
        return None

